/**
 * 医生预约管理初始化
 */
var DoctorPoint = {
    id: "DoctorPointTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    yourRoleId: 5
};

/**
 * 初始化表格的列
 */
DoctorPoint.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '编号', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '预约居民身份证', field: 'patientIdcard', visible: true, align: 'center', valign: 'middle'},
            {title: '居民姓名', field: 'patientName', visible: true, align: 'center', valign: 'middle'},
            {title: '医生姓名', field: 'doctorName', visible: true, align: 'center', valign: 'middle'},
            {title: '预约时间', field: 'pointDate', visible: true, align: 'center', valign: 'middle'},
            {title: '预约地点', field: 'pointPlace', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
DoctorPoint.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        DoctorPoint.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加医生预约
 */
DoctorPoint.openAddDoctorPoint = function () {
    var index = layer.open({
        type: 2,
        title: '添加医生预约',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorPoint/doctorPoint_add'
    });
    this.layerIndex = index;
    DoctorPoint.searchDoctorName();
};

/**
 * 打开查看医生预约详情
 */
DoctorPoint.openDoctorPointDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '医生预约详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/doctorPoint/doctorPoint_update/' + DoctorPoint.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除医生预约
 */
DoctorPoint.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/doctorPoint/delete", function (data) {
            Feng.success("删除成功!");
            DoctorPoint.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("doctorPointId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询医生预约列表
 */
// DoctorPoint.search = function () {
//     var queryData = {};
//     queryData['condition'] = $("#condition").val();
//     DoctorPoint.table.refresh({query: queryData});
// };
DoctorPoint.search = function () {
    var queryParam = $('#condition').val(); // 获取输入框的值
    var table = $('#' + this.id);

    if (queryParam) {
        table.bootstrapTable('filterBy', {
            patientName: [queryParam]  // 使用输入框的值作为过滤条件（这里假设过滤条件是居民姓名）
        });
    } else {
        // 如果没有输入查询参数，则显示全部数据
        table.bootstrapTable('filterBy', {});
    }
};


/**
 * 获取医生名字
 */
// 绑定选择医生下拉框的点击事件
$(document).ready(function() {
    DoctorPoint.searchDoctorName();
});

DoctorPoint.searchDoctorName = function () {
    console.log("Fetching doctor names...");

    // 创建XMLHttpRequest对象
    var xhr = new XMLHttpRequest();

    // 设置请求方法和URL
    xhr.open('GET', Feng.ctxPath + '/doctorPoint/getDoctorName?rolid=' + DoctorPoint.yourRoleId, true);

    // 设置回调函数
    xhr.onreadystatechange = function () {
        console.log("ReadyState: " + xhr.readyState + ", Status: " + xhr.status); // 调试信息

        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                // 请求成功，处理响应数据
                var doctorNames = JSON.parse(xhr.responseText);
                console.log("Received doctor names:", doctorNames); // 调试信息
              const doctorName = document.querySelector("#doctorName");
                // 清空select选项
                // $('#doctorName').empty();
                doctorName.innerHTML = "${item.doctorName}";

                // 添加医生名字到select选项中
                for (let i = 0; i < doctorNames.length; i++) {
                    // var option = '<option value="' + doctorNames[i] + '">' + doctorNames[i] + '</option>';
                    var option = document.createElement(`option`)
                    // $('#doctorName').append(option);
                    option.value = doctorNames[i];  // 设置option的value属性
                    option.textContent = doctorNames[i];  // 设置option的文本内容
                    doctorName.appendChild(option);
                }
                // 设置初始值为item.doctorName
                // doctorName.value = "${item.doctorName}";
            } else {
                console.error("Failed to fetch doctor names. Status code:", xhr.status); // 错误信息
            }
        }
    };

    // 发送请求
    xhr.send();
};

//调用searchDoctorName方法
DoctorPoint.doctorClick = function (){
    console.log("Doctor select clicked!"); // 你的逻辑代码
    DoctorPoint.searchDoctorName();
};

/**
 * 初始化表格和其他功能
 */
$(function () {
    var defaultColunms = DoctorPoint.initColumn();
    var table = new BSTable(DoctorPoint.id, "/doctorPoint/list", defaultColunms);
    table.setPaginationType("client");
    DoctorPoint.table = table.init();
});
